<template>
  <view>
    <view class="search-box"> 
      <my-search bgColor="#c00000" :radius="15" @click="goToSearch"></my-search>
    </view>
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item,i) in swiperList" :key="i">
        <!-- 轮播图区域点击轮播图跳转商品相应的详情页 -->
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id"
          open-type="navigate">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- nav区域 -->
    <view class="nav_list">
      <view class="nav_item" v-for="(item, index) in navList" :key="index" @click="navClickHandler(item)">
        <image :src="item.image_src"></image>
      </view>
    </view>
    <!-- 楼层区域 -->
    <view class="floor_list">
      <view class="floor_item" v-for="(item,index) in floorList" :key="index">
        <!-- <view class="floor_title"> -->
        <image class="floor_title" :src="item.floor_title.image_src"></image>
        <!-- </view> -->
        <view class="product_list">
          <view class="product_left">
            <navigator class="swiper-item" :url="item.product_list[0].url"
              open-type="navigate">
              <image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
            </navigator>
          </view>
          <view class="product_right_box">
            <view class="product_right" v-for="(item2,index2) in item.product_list.slice(1)" :key="index2">
             <navigator class="swiper-item" :url="item2.url"
               open-type="navigate">
                <image :src="item2.image_src" :style="{width:item2.image_width + 'rpx'}" mode="widthFix"></image>
              </navigator>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>
<script>
  import {
    $http
  } from '@escook/request-miniprogram';

  export default {
    data() {
      return {
        swiperList: [],
        navList: [],
        floorList: []
      };
    },
    onLoad() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },
    methods: {
      // 获取轮播图区域
      async getSwiperList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/swiperdata')
        if (res.meta.status !== 200) return uni.$showMsg()
        // {
        // return uni.showToast({
        //   title: '数据请求失败',
        //   duration: 1500,
        //   icon: 'none'
        // })
        // }
        this.swiperList = res.message
        // uni.$showMsg('数据请求成功')
      },
      // 获取nav导航部分
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.navList = res.message
      },
      // 点击分类导航跳转分类页面
      navClickHandler(item) {
        if (item.name == '分类') {
          uni.switchTab({
            url: "/pages/cate/cate"
          })
        }
      },
      // 获取楼层区域列表
      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/floordata')
        if (res.meta.status !== 200) return uni.$showMsg()
        res.message.forEach(floor =>{
          floor.product_list.forEach(prod =>{
            prod.url = '/subpkg/goods_list/goods_list?'+ prod.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message
      },
      goToSearch(){
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  .search-box{
    // 设置定位效果为吸顶
    position: sticky;
    // 吸顶的位置
    top:0;
    // 提高层级，防止被轮播图覆盖
    z-index:999;
  }
  swiper {
    height: 330rpx;

    swiper-item {
      image {
        width: 100%;
      }
    }
  }

  .nav_list {
    display: flex;
    justify-content: space-between;
    margin: 15px 0;

    image {
      width: 128rpx;
      height: 140rpx;
    }
  }
  .floor_title{
    height: 60rpx;
    width: 100%;
    display: flex;
  }
  .product_list{
    display: flex;
  }
  .product_left{
    padding-left: 10rpx;
  }
  .product_right_box{
    display: flex;
    // 自动换行
    flex-wrap: wrap;
    justify-content: space-around;
  }
</style>